<template>
  <div class="kindeditor">
    <textarea
      id="aaa"
      class="form-control"
      ref="kindeditor"
      v-model="localValue"
      name="content"
      v-loaded-callback="initKindeditor"
    ></textarea>
    <!-- <el-button type="primary" @click="test">立即创建</el-button> -->
  </div>
</template>

<script>
// import '../../../node_modules/kindeditor/kindeditor-all.js'
// import '../../../node_modules/kindeditor/lang/zh-CN.js'
// import '../../../node_modules/kindeditor/themes/default/default.css'
import "kindeditor/kindeditor-all";
import "kindeditor/lang/zh-CN";
import "kindeditor/themes/default/default.css";
export default {
  name: "kindeditor",
  props: ["options", "value"],
  data() {
    return {
      localValue: "",
      ke: false,
      changeKe: true
    };
  },
  watch: {
    localValue: "updateValue",
    value: "setDefault"
  },
  created() {
    this.setDefault();
  },
  methods: {
    initKindeditor() {
      var _this = this;
      // 默认参数
      var options = {
        width: "100%",
        afterChange() {
          _this.localValue = this.html();
          _this.$emit("change", _this.localValue);
        },
        afterBlur() {
          _this.changeKe = true;
        },
        afterFocus() {
          _this.changeKe = false;
        },
        filePostName: 'file',//跟服务端参数对应
        allowFileManager: true,
        uploadJson: this.$api+"/upload_json.php",
        fileManagerJson: this.$api+"/file_manager_json.php",

        resizeType: 1,
        urlType: "domain",
        syncType: ""
      };
      // 调用外部参数
      if (_this.options) {
        for (var i in _this.options) {
          options[i] = _this.options[i];
        }
      }
      this.ke = KindEditor.create(_this.$refs.kindeditor, options);
    },
    // 设置初始值
    setDefault() {
      this.localValue = this.value;
      if (this.ke && this.changeKe) this.ke.html(this.localValue);
      // console.log(this.ke, this.changeKe);
    },
    // 修改父件的值
    updateValue() {
      this.$emit("input", this.localValue);
    },
    test() {
      // this.ke.html("123sdf");
      console.log('/static/redirect.html');
      console.log(this.ke.uploadJson);
    }
  }
};
</script>